Repository navigation
Extracting a coding session that holds nothing to remember costs a fraction of a cent - #1676
Merged
Merged
Conversation
…action of a cent A coding plugin's session capture was extracted with the conversation prompt and a block appended that replaced its code rules: 20,699 system tokens for a short capture and 34,294 for a longer one, for 300 to 770 tokens of session. Most captures hold nothing to keep, so that prefix was the price of the call, and a capture that arrived after the provider's five-minute prompt cache had expired paid to write it again. A session capture now has a prompt of its own, about 2,500 tokens at every capture size: the coding-session rules as they were, what a user states that holds beyond the session, the existing-memory rule, and a schema with the fields a coding fact uses. It is sent without the examples preamble, which binds a reply to numbered examples the prompt does not have. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
The session prompt's schema asks for `valid_to` and `foresight`, the two fields that end a row's validity. Without them a state the user bounds in a coding session ("the release branch is frozen until 5 December") was written as a standing `semantic` memory with no end, because the parser defaults a field the schema does not name. The write step now gets a `foresight` window for it and writes a `foresight` row whose `valid_to` is the stated end. The prompt is about 2,700 tokens.
A unit test names every schema field the write step acts on, so removing `forget`, `linked_keys`, `about`, `valid_to` or `foresight` from the schema fails it, and the prompt test pins the system text of a capture that is shown existing memories: the same prompt followed by the fence note. The feature page says which fields the extractor leaves unset and where the row's `valid_at` then comes from.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 14e8f4c. Configure here.
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
9 of 10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
Extracting a coding session that a plugin captured costs under a cent when the provider's prompt cache is cold, where it cost 5 to 9 cents, and about a fifth of a cent when it is warm. A session capture is now read with a prompt of its own, about 2,700 tokens, instead of the 20,000 to 34,000-token conversation prompt.
Why
A measured run of 14 coding sessions with the plugin installed, in which the agent called no Loom tool, ended each session with one server-side extraction call. The provider's usage records for those 14 calls, at list prices of $2 per million input tokens, $2.50 for a five-minute cache write, $0.10 for a cache read and $10 for output:
13 of the 14 captures wrote no memory. The system prompt was about 98% of every request, and 88% of the spend was writing or reading it.
The cause is which prompt a session capture got. It was the conversation prompt, whose rules and 23 or 49 examples are about people, purchases, dates and recommendations, with a block appended that replaced its code rules for a coding session. A capture over 1,200 characters got the full prompt and a shorter one the compact prompt, so session captures kept two prefixes warm, not one. The provider caches a prefix for five minutes, so a capture that arrived later than that after the previous one of its size paid to write the prefix again.
A session capture now has a prompt of its own, the same at every capture size. A capture that is shown existing memories gets the fence note after it, as every extraction does, and only a capture of 1,200 characters or fewer is shown them. So a namespace with no stored memories uses one cached prefix where it used two, and a namespace that holds memories still uses two, each about 2,700 tokens. The prompt holds:
valid_to,foresight), ending with the example's fact as JSON.It is sent without the examples preamble. That preamble makes numbered examples binding and rules out judging whether a case is worth a fact, and this prompt has no numbered examples and asks for that judgement.
Three alternatives were measured or worked out and left out:
Scope and risk
extra_tagswithsource:claude-code,source:codexorsource:cursor). Conversations, trajectory replays, the durability verifier, the retry rules and the coverage sweep are unchanged.key,value,tags,entities,topics,linked_keys,source_message_indices,temporal_anchor,valid_to,foresight,relations,aboutandforget. It no longer asks fortemporal_confidence,valid_at,derivedorquestion_topic, which the parser defaults. The extractor therefore sets novalid_ator occurrence record on a session capture's fact; the row takes itsvalid_atfrom the fact'stemporal_anchorwhen that is not in the future, else from the observation date.valid_toandforesightstay, so a state the user bounds ("frozen until 5 December") ends on that day and is not served after it.relationsstays, so a fact with two entities does not cost the relation backfill call.valid_to) or a forget request, so those paths of the new prompt are covered by its text alone.Compatibility, rollout and rollback
Tests
Offline replay, no provider call. The 14 recorded captures were rebuilt from the session transcripts with the plugin's turn selection, and the request was built with the server's own functions (
_extraction_system_prompt,_build_extraction_user_prompt, the provider client's payload builder). Onmainthe rebuilt requests match the recorded ones byte for byte (65,876 to 66,203 and 103,797 to 104,165 bytes) and token for token, counted with the provider's token-count endpoint. At the head commit:LOOM_ENV_FILE= python -c "from server.memory.domain.extraction_prompts import _CODING_SESSION_SYSTEM_PROMPT as p; print(len(p))"prints 8058 characters.Paid replay on the configured extraction model, about $0.91 of provider calls in all. Each capture went through the server's own stages (
loom.llm_extract,loom.drop_activity_narration,loom.drop_non_durable,loom.verify_durable) with an empty namespace. At the head commit:main, two runs: the same three facts.main, two runs each: one fact extracted in four of six runs and dropped by the verifier each time, no fact kept.foresightandvalid_to, two runs: the fact came back with neither, and the row wassemanticwith novalid_to. At the head commit, three runs:foresightis{"until": "2026-12-05", "duration_days": null}and the row isforesightwithvalid_to2026-12-05 in every run. The sentence is not one of the prompt's examples.valid_toandforesight; with this one that session returned no fact in its one run.Not verified: the comparison has two to twelve runs per session, on one repository and one model, so a difference in how often a borderline fact is kept would not show unless it is large. No session with existing memories was replayed, so the existing-memory rule,
linked_keysand the prompt followed by the fence note are untested against a model. One bounded statement was replayed, with a date; a stated length ("for two weeks") and a past end (valid_to) were not.Unit tests.
tests/unit/test_coding_session_extraction.pynow checks that a session capture's system prompt is the session prompt alone at both capture sizes, with no conversation example and no preamble, and the same prompt followed by the fence note when existing memories are shown; that it stays under 9,000 characters; that the schema names every field the write step acts on (removing theforget,linked_keys,about,valid_toorforesightline fails it, checked by removing each); that the JSON the schema ends with parses to a complete fact; and that a coverage sweep's prompt takes the schema without the rules. No pytest suite was run locally. The bodies of every test in that file (30 cases) and of the sweep-prompt, prompt-fence, package-contract, doc-link and public-copy tests pass when run as a plain script; CI runs the suites.Gates.
ruff format --check,ruff check, the function-length and secrets gates, every generator with--check, Prettier on the changed page: clean. The mypy gate reports five errors inserver/llm/vertex_auth.py, a file this change does not touch, from untyped imports in the local environment, and none elsewhere.AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code. Checked: that the rebuilt requests equal the recorded ones before the change, the token counts and costs of every replayed call, and the facts each replay kept.
I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make test-unitand applicable integration results are reported by CI; a full local run is not required before pushing.docs/reference/configuration.mdand thedocs/reference/tables (python scripts/codegen/docs_reference.py), the reach manifest,schemas/schema.sql(make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).main; no merged migration is edited.hooks/,lib/,bin/,skills/,.mcp.json, the Codexplugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code
Note
Medium Risk
Changes extraction behavior and which facts survive for plugin session captures only; conversation and trajectory paths are unchanged, but borderline kept/dropped facts may shift.
Overview
Plugin session captures (
source:claude-code,codex,cursor) now use a standalone extraction system prompt (~2,700 tokens) at every capture size, instead of the full or compact conversation prompt (20k–34k tokens) with a coding block appended. That cuts provider input cost on the common case where most captures write no facts.The wiring changes:
_extraction_base_promptreturns_CODING_SESSION_SYSTEM_PROMPTwhencoding_sessionis true; the strict-examples preamble is applied only for conversation extracts, not session captures. The new prompt is self-contained—user durable facts, reusable codebase rules, existing-memory dedup, and a narrower output schema—without numbered conversation examples.The session schema still includes fields the write path needs (including
valid_to,foresight,relations,forget); fields likevalid_at,temporal_confidence,derived, andquestion_topicare omitted and rely on parser defaults. Documentation and unit tests assert prompt isolation, size, schema coverage, and parser compatibility with the embedded example JSON.Reviewed by Cursor Bugbot for commit 69f4042. Bugbot is set up for automated code reviews on this repo. Configure here.